package org.ume.school.crawl;

import org.ume.school.modules.acticles.ArticleRepository;
import org.ume.school.modules.model.entity.Goods;
import de.nava.informa.core.ParseException;
import de.nava.informa.impl.basic.Category;
import de.nava.informa.impl.basic.Channel;
import de.nava.informa.impl.basic.ChannelBuilder;
import de.nava.informa.impl.basic.Item;
import de.nava.informa.parsers.FeedParser;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;

/**
 * 启动后定时去爬取RSS资源
 * Created by Django on 2017/7/21.
 */
@Component
public class CrawlRobot {


    @Resource
    private ArticleRepository repository;

    public void rss(String rss, String type) throws IOException, ParseException {
        ChannelBuilder builder = new ChannelBuilder();
        String url = rss;
        Channel channel = (Channel) FeedParser.parse(builder, url);
        for (Object x : channel.getItems()) {
            Item item = (Item) x;
            Goods article = new Goods();
            article.setId(UUID.randomUUID().toString());
            article.setContent(item.getDescription());
            article.setTitle(item.getTitle());
            article.setCreateTime(item.getDate());
            article.setSource(item.getLink().toString());
            article.setUserName(item.getCreator());
            Iterator<Category> iterator = item.getCategories().iterator();
            StringBuffer sb = new StringBuffer("#");
            while (iterator.hasNext()) {
                sb.append(iterator.next().getTitle()).append("#");
            }
            article.setTags(sb.toString());
            article.setType("1");
            int len = article.getContent().length();
            if (len > 100) {
                len = 100;
            }
            article.setSummary(article.getContent().substring(0, len));
            repository.save(article);
        }
        repository.flush();
    }
}
